# -*- coding: utf8 -*-
from twitter import Api
import simplejson
import urllib2
import logging

class Api(Api):
    ''' twitter.Apiクラスの拡張
        self._cacheの影響でファイル入出力が発生するため、
        Apiクラスのラッパーとして利用する。
    '''
    def __init__(self,
                 username=None,
                 password=None,
                 input_encoding=None,
                 request_headers=None):
        '''gaetwitter.Api オブジェクトの初期化

        Args:
        username: The username of the twitter account.  [optional]
        password: The password for the twitter account. [optional]
        input_encoding: The encoding used to encode input strings. [optional]
        request_header: A dictionary of additional HTTP request headers. [optional]
        '''
        self._cache = None
        self._urllib = urllib2
        self._cache_timeout = Api.DEFAULT_CACHE_TIMEOUT
        self._InitializeRequestHeaders(request_headers)
        self._InitializeUserAgent()
        self._InitializeDefaultParameters()
        self._input_encoding = input_encoding
        self.SetCredentials(username, password)

    def Search(self, q=None):
        parameters = {}
        if q:
            parameters['q'] = q
        url = 'http://search.twitter.com/search.json'
        json = self._FetchUrl(url, parameters=parameters)
        data = simplejson.loads(json)
        self._CheckForTwitterError(data)
        return [Search.NewFromJsonDict(x) for x in data['results']]

class Search(object):
    def __init(self,
               text=None):
        self.text = text
    @staticmethod
    def NewFromJsonDict(data):
        if 'from_user' in data:
            return data['from_user']
